﻿namespace CRM.DataAccess.DAOs
{
    using System.Collections.Generic;
    using System.Data.Entity;
    using CRM.DataAccess.Entities;
    using System.Linq;
    using System.Data.Entity.Validation;
    using System.Diagnostics;
    using System;

    public class RoleDAO : DbContext
    {
        public RoleDAO()
            : base("name=Entities")
        {
        }
        public DbSet<ROLE> ROLES { get; set; }
        public DbSet<USER> USERS { get; set; }

        //Get Role
        public List<ROLE> GetRoles()
        {
            try
            {
                var roleQuery = from r in ROLES
                                where r.ACTIVE
                                select r;
                List<ROLE> roles = roleQuery.ToList();
                if (roles.Count > 0)
                    return roles;
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Check exist
        public int CheckExist(string RoleName)
        {
            try
            {
                var Query = from u in ROLES
                            where u.ROLE_NAME.ToUpper() == RoleName.ToUpper()
                            select u;
                List<ROLE> rs = Query.ToList();
                if (rs.Count > 0)
                    return 1;
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //Add new Role
        public int AddNewRole(ROLE role)
        {
            try
            {
                this.ROLES.Add(role);
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        //Get Role by Condition
        public List<ROLE> GetRoleByCondition(string RoleName, bool? isActive, bool? isManager)
        {
            try
            {
                var Query = from role in ROLES
                            where ((RoleName != "" ? (role.ROLE_NAME.ToUpper().Contains(RoleName.ToUpper())) : (true))
                            //(role.ROLE_NAME.ToUpper() == (RoleName == "" ? role.ROLE_NAME.ToUpper() : RoleName.ToUpper())
                            && role.ACTIVE == (isActive == null ? role.ACTIVE : isActive)
                            && role.IS_MANAGER == (isManager == null ? role.IS_MANAGER : isManager))
                            select role;
                List<ROLE> rs = Query.AsNoTracking().ToList();
                if (rs.Count > 0)
                    return rs;
                return null;
            }
            catch
            {
                return null;
            }
        }
        //Delete Role
        public int DeleteRole(long ID)
        {
            try
            {
                ROLES.RemoveRange(ROLES.Where(x => x.ID == ID));
                this.SaveChanges();
                return 1;
            }
            catch
            {
                return -1;
            }
        }
        //Get Role by ID
        public ROLE GetRoleByID(long ID)
        {
            try
            {
                var RoleQuery = from u in ROLES
                                where u.ID == ID
                                select u;
                List<ROLE> Roles = RoleQuery.ToList();
                if (Roles.Count > 0)
                    return Roles[0];
                return null;
            }
            catch
            {
                return null;
            }
        }
        //update Role
        public int UpdateRole(ROLE role)
        {
            try
            {
                var original = ROLES.Find(role.ID);
                if (original != null)
                {
                    Entry(original).CurrentValues.SetValues(role);
                    SaveChanges();
                    return 1;
                }
                return -1;
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
                return -1;
            }
        }
        
    }
}
